angular.module('mamApp').controller('commonTagController', ['$scope', 'TagsManager', '$state', '$uibModal', 'ImsDialog',
    function ($scope, TagsManager, $state, $uibModal, ImsDialog) {
        $scope.search = {
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10
        }

        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search)
            }
        }
        $scope.noPermission=false;
        TagsManager.commonDimensions([$scope.search.pageNo, $scope.search.pageSize]).then(function (result) {
            if(result.data.c=="n"){
                $scope.noPermission=true;
            }else{
                $scope.noPermission=false;
                $scope.commonDimensionList = result.data.pageList;
                $scope.paginationConf.totalItems = result.data.total;
            }



        })
        //全选

        $scope.allCheckChange = function (allCheck) {
            $scope.commonDimensionList.forEach(function (item) {
                item.checked = allCheck;
            })
        }

        //单个删除
        $scope.deleteCommonDimens = function (id) {
            ImsDialog.open({title: "纬度删除", message: "确定删除该标签纬度？"}).then(function (a) {
                if (a == "ok") {
                    TagsManager.commonDimensionsDelete([id]).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        }

        var selectIds = function () {
            var ids = [];
            $scope.commonDimensionList.forEach(function (item) {
                if (item.checked) {
                    ids.push(item.dimensionId);
                }
            })
            return ids;
        }

        //但个修改状态
        $scope.selectChangeStatus = function (status) {

            if (!selectIds().length) {
                return false;
            }
            ImsDialog.open({
                title: "纬度" + (status == 2 ? '禁用' : '启用'),
                message: "确定" + (status == 2 ? '禁用' : '启用') + "选中的标签纬度？"
            }).then(function (a) {
                if (a == "ok") {
                    TagsManager.commonDimensionsChangeStatus(selectIds(), {status: status}).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        }


        $scope.itemCheckedChange = function () {

            if (selectIds().length == $scope.commonDimensionList.length) {
                $scope.allChecked=true;
            }else{
                $scope.allChecked=false;
            }

        }


        //删除选中的
        $scope.selectDelete = function () {
            if (!selectIds().length) {
                return false;
            }

            if (!selectIds().length) {
                return false;
            }
            ImsDialog.open({title: "纬度删除", message: "确定删除选中的标签纬度？"}).then(function (a) {
                if (a == "ok") {
                    TagsManager.commonDimensionsDelete(selectIds()).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        }

        //添加纬度
        $scope.addCommonDimensions = function (commonDimens) {
            var modal = $uibModal.open({
                templateUrl: 'commonDimensModal.html',
                controller: 'commonDimensionModalController',
                size: 'normal',
                openedClass:"modal-open-normal",
                resolve: {
                    param: function () {
                        return {commonDimens: commonDimens};
                    }
                }
            });
            modal.result.then(function (result) {
                if (result == "ok") {
                    setTimeout(function () {
                        $state.reload();
                    }, 200)
                }
            });
        }


        //修改纬度状态
        $scope.changeStatus = function (commonDimens) {
            var status = 2;
            if (commonDimens.status == 2) {
                status = 4;
            } else {
                status = 2;
            }
            ImsDialog.open({
                title: "纬度" + (status == 2 ? '禁用' : '启用'),
                message: "确定" + (status == 2 ? '禁用' : '启用') + "该标签纬度？"
            }).then(function (a) {
                if (a == "ok") {
                    TagsManager.commonDimensionsChangeStatus([commonDimens.dimensionId], {status: status}).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })

        }

    }])

